package com.atom.common.activiti;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.task.Task;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.atom.common.dao.RunningTaskDao;
import com.atom.common.delegate.TaskCreateDelegateHandler;
import com.atom.common.entity.RunningTaskDO;
import com.atom.common.exception.AvensIllegalArgumentException;
import com.atom.common.exception.AvensRemoteException;
@Component
public class RunningTaskHandler {

  private static Logger logger = Logger.getLogger(RunningTaskHandler.class);

  @Resource
  private RunningTaskDao dao;
  @Resource
  private TaskCreateDelegateHandler taskCreateDelegateHandler;
	@Resource
	private ActivitiHandler activitiHandler;
  /**
	 * getDbListByUser 根据userID获取所有活动受理人为此userID的任务
	 * 
	 * @author wangli
	 * @param userid
	 * @return
	 */
	public List<RunningTaskDO> getListByUser(String userid) {
		try {
			List<RunningTaskDO> list = dao.findByProperty(
					"assignee", userid);
			return list;
		} catch (Exception e) {
			return null;
		}
	}
	public List<RunningTaskDO> getListByUser(String userid,String taskDefinitionKey) {
		try {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("assignee", userid);
			map.put("taskDefinitionKey", taskDefinitionKey);
			List<RunningTaskDO> list = dao.findByProperties(map);
			return list;
		} catch (Exception e) {
			return null;
		}
	}
	
	public List<RunningTaskDO> getListByTaskId(String taskid) {
		try {
			List<RunningTaskDO> list = dao.findByProperty("taskId", taskid);
			return list;
		} catch (Exception e) {
			return null;
		}
	}
	
	public List<RunningTaskDO> getListByUserAndPage(String userid, int start, int count) {
		try {
			List<RunningTaskDO> list = dao.findByPropertyAndPageAndOrder(
					"assignee", userid, start, count ,"createTime", "desc");
			return list;
		} catch (Exception e) {
			return null;
		}
	}
	
	public List<RunningTaskDO> getListByUserAndPage(String userid,String taskDefinitionKey, int start, int count) {
		try {
			Map<String,String> map = new HashMap<String,String>();
			map.put("assignee", userid);
			map.put("taskDefinitionKey", taskDefinitionKey);
			List<RunningTaskDO> list = dao.findByMapAndPageAndOrder(map, start, count ,"createTime", "desc");
			return list;
		} catch (Exception e) {
			return null;
		}
	}

	public List<RunningTaskDO> getListByUserAndTitle(String userid, String title) {
		try {
			List<RunningTaskDO> list = dao.findByPropertys(
					"assignee", "processInstanceName", userid, title );
			return list;
		} catch (Exception e) {
			return null;
		}
	}
	public List<RunningTaskDO> getListByUserAndTitle(String userid, String title, String taskDefinitionKey) {
		try {
			List<RunningTaskDO> list = dao.findByPropertys(
					"assignee", "processInstanceName","taskDefinitionKey", userid, title, taskDefinitionKey);
			return list;
		} catch (Exception e) {
			return null;
		}
	}

	public List<RunningTaskDO> getListByUserAndTitleAndPage(String userid, String title,String taskDefinitionKey, int start, int count) {
		try {
			List<RunningTaskDO> list = dao.findByPropertysAndPageAndOrder(
					"assignee", "processInstanceName","taskDefinitionKey", userid, title,taskDefinitionKey, start, count,"createTime","desc");
			return list;
		} catch (Exception e) {
			return null;
		}
	}
	public List<RunningTaskDO> getListByUserAndTitleAndPage(String userid, String title, int start, int count) {
		try {
			List<RunningTaskDO> list = dao.findByPropertysAndPageAndOrder(
					"assignee", "processInstanceName", userid, title, start, count,"createTime","desc");
			return list;
		} catch (Exception e) {
			return null;
		}
	}

  
  
	/**
	 * Description: 写入待办表
	 * 
	 * @param task
	 * @param variables
	 * @param linkUser
	 * @param ownerId
	 */
	public void insert(Task task, Map<String, Object> variables, String linkUser) {
		String assignee = linkUser;
		String owner = linkUser;
		try {
			// 写本地ATOM待办
			RunningTaskDO ru = RunningTaskDO.getRunningTaskDO(task, variables, assignee, owner);
			dao.save(ru);
		} catch (Exception e) {
			logger.error("error message = {}" + e.getMessage(), e);
			throw new AvensRemoteException("InsertRunning Failed");
		}
	}

	/**
	 * Description: 写入ATOM待办表,CollectCreateListener用
	 * 
	 * @param task
	 * @param variables
	 * @param owner
	 */
	public void insert(DelegateTask task, Map<String, Object> variables, String owner) {
		try {
			String[] users = taskCreateDelegateHandler.execute(task, variables, owner);
			for (String user : users) {
				// 写ATOM待办
				RunningTaskDO ru = RunningTaskDO.getRunningTaskDO(task,variables, user, owner);
				dao.save(ru);
			}
		} catch (Exception e) {
			logger.error("error message = {}" + e.getMessage(), e);
			throw new AvensRemoteException("InsertRemote Failed");
		}
	}


	  public void updateTaskName(String taskId, String taskName) {
			// 更新ATOM待办
			dao.updateTaskName(taskId, taskName);
		}

  /**
   * Description: 删除已提交任务
   * @param taskId
   */
  public void delete(String taskId, String userId, Map<String, Object> var_task) {
    if (taskId == null) {
      throw new AvensIllegalArgumentException("Argument is null(taskId:" + taskId + ")");
    }
    dao.delete(taskId, userId);
  }
  
  public RunningTaskDO getLastByProcessIdAndTaskKey(String processId, String taskKey){
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("processInstanceId", processId);
		map.put("taskDefinitionKey", taskKey);
		List<RunningTaskDO> list = dao.findByProperties(map);
		if(list!=null&&list.size()>0){
			return list.get(list.size()-1);
		}
		return null;
	}
  
  public void lockLocal(String taskId, String assignee) {
	  dao.updateLock(taskId, assignee, 1);

	}

	public void unLockLocal(String taskId, String assignee) {
		dao.updateLock(taskId, assignee, 0);

	}
	
}
